package com.yjs.app.trade.service.service.business.order.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yjs.app.trade.core.base.mybatis.dbutil.DynamicSwitchDataSource;
import com.yjs.app.trade.core.util.StringUtils;
import com.yjs.app.trade.entity.base.util.Page;
import com.yjs.app.trade.entity.bean.BacthInfo;
import com.yjs.app.trade.service.mapper.business.order.OrderMapper;
import com.yjs.app.trade.service.service.business.order.OrderService;
import com.yjs.app.trade.service.service.business.search.NewSearchListService;
import com.yjs.app.trade.service.service.util.bean.OrderDetailInfo;

import net.sf.json.JSONObject;

/**
 * 订单业务层相关接口实现
 * 
 * @author Administrator
 *
 */
@Service("orderService")
public class OrderServiceImpl implements OrderService {

	@Autowired
	OrderMapper orderMapper;

	@Autowired
	NewSearchListService newSearchListService;
	
	@Override
	public List<BacthInfo> findBacthsByOrderIds(List<String> list) {
		return orderMapper.findBacthsByOrderIds(list);
	}

	@Override
	@DynamicSwitchDataSource(dataSource = "stock")
	public List<Map<String, Object>> findShareNumberByDispatchIdAndDrugCode(List<String> contractIds,List<String> drugCodes) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("contractIds", contractIds);
		map.put("drugCodes", drugCodes);
		return orderMapper.findShareNumberByDispatchIdAndDrugCode(map);
	}

	@Override
	public Page<?> findOrderByBacthNo(JSONObject jsonObject, String memberId,Page<OrderDetailInfo> page, OrderDetailInfo orderDetailInfo) {
		if (StringUtils.strIsNotNull(jsonObject.get("bacthNo"))) {
			orderDetailInfo.setBacthNo(jsonObject.getString("bacthNo"));
		}
		if (StringUtils.strIsNotNull(jsonObject.get("sellerName"))) {
			orderDetailInfo.setSellerName(jsonObject.getString("sellerName"));
		}
		if (StringUtils.strIsNotNull(jsonObject.get("dispatcherName"))) {
			orderDetailInfo.setDispatcherName(jsonObject.getString("dispatcherName"));
		}
		if (StringUtils.strIsNotNull(jsonObject.get("orderNo"))) {
			orderDetailInfo.setOrderNo(jsonObject.getString("orderNo"));
		}
		if (StringUtils.strIsNotNull(jsonObject.get("producter"))) {
			orderDetailInfo.setProducter(jsonObject.getString("producter"));
		}
		if (StringUtils.strIsNotNull(jsonObject.get("productCode"))) {
			orderDetailInfo.setProductCode(jsonObject.getString("productCode"));
		}
		if (StringUtils.strIsNotNull(jsonObject.get("beginDt"))) {
			orderDetailInfo.setBeginDt(jsonObject.getString("beginDt"));
		}
		if (StringUtils.strIsNotNull(jsonObject.get("endDt"))) {
			orderDetailInfo.setEndDt(jsonObject.getString("endDt"));
		}
		if (StringUtils.strIsNotNull(jsonObject.get("productName"))) {
			if (StringUtils.isAllChinese(jsonObject.getString("productName"))) {
				orderDetailInfo.setProductName(jsonObject.getString("productName"));
			} else {
				orderDetailInfo.setProductNamePY(jsonObject.getString("productName"));
			}
		}
		orderDetailInfo.setBuyerId(memberId);
		orderDetailInfo.setPage(page);
		List<OrderDetailInfo> list = orderMapper.findOrderByBacthNo(orderDetailInfo);
		page.setList(list);
		return page;
	}

	@Override
	public Map<String, Object> findOrderMemberId(String orderNumber) {
		return orderMapper.findOrderMemberId(orderNumber);
	}

	@Override
	public Map<String, Object> countOrderInfoByOrderInfoId(Map<String, Object> map) {
		return orderMapper.countOrderInfoByOrderInfoId(map);
	}

	@Override
	public List<Map<String, String>> findOrderNumber(String orderNo) {
		List<Map<String, String>> findOrderNumbers = orderMapper.findOrderNumber(orderNo);
		return findOrderNumbers;
	}

	@Override
	public Map<String, Object> findDispatchTodayTotalOrderInfo(String dispatchId) {
		Map<String, Object> map = orderMapper.findDispatchTodayTotalOrderInfo(dispatchId);
		if (map != null && map.size() > 0) {
			return map;
		} else {
			map = new HashMap<>();
			map.put("orderNumber", 0);
			map.put("productOrderNum", 0);
			return map;
		}
	}

	@Override
	public String findOrderDetailInfoByOrderDetailNo(String orderDetailNo) {
		return orderMapper.findOrderDetailInfoByOrderDetailNo(orderDetailNo);
	}

	@Override
	public Map<String, Object> findDispatchPassportTodayTotalOrderInfo(String dispatchId, String passportId) {
		Map<String, Object> map = new HashMap<>();
		map.put("dispatchId", dispatchId);
		map.put("passportId", passportId);
		Map<String, Object> result = orderMapper.findDispatchPassportTodayTotalOrderInfo(map);
		if (result != null && result.size() > 0) {
			return result;
		} else {
			result = new HashMap<>();
			result.put("orderNumber", 0);
			result.put("productOrderNum", 0);
			return result;
		}
	}

	@Override
	public Map<String, Object> findSellerTodayTotalOrderInfo(String sellerId) {
		Map<String, Object> fi = orderMapper.findSellerTodayTotalOrderInfo(sellerId);
		if(fi == null || fi.size()<0){
			fi=new HashMap<>();
			fi.put("sellerId", "-");
			fi.put("orderNumber", 0);
			fi.put("orderNum", 0);
			fi.put("productName", "-");
			fi.put("productOrderNumber", 0);
			fi.put("productOrderNum", 0);
		}
		return fi;
	}
	
	@Override
	public Map<String, Object> findSellerPassportTodayTotalOrderInfo(String sellerId, String passportId) {
		Map<String, Object> map = new HashMap<>();
		map.put("sellerId", sellerId);
		map.put("passportId", passportId);
		return orderMapper.findSellerPassportTodayTotalOrderInfo(map);
	}
}
